package com.myl.web.controller.system;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.myl.domain.system.Dept;
import com.myl.domain.system.Role;
import com.myl.service.system.RoleService;
import com.myl.service.system.impl.RoleServiceImp;
import com.myl.utils.BeanUtil;
import com.myl.web.controller.BaseServlet;

/**
 * Servlet implementation class RoleServlet
 */
@WebServlet("/system/role")
public class RoleServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public RoleServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String operation = request.getParameter("operation");
		if("list".equals(operation)){
			
			this.list(request,response);
			
		}else if ("toAdd".equals(operation)) {
			this.toAdd(request, response);
		}else if ("save".equals(operation)) {
			this.save(request, response);
		}else if ("toEdit".equals(operation)) {
	         this.toEdit(request,response);
        }else if ("edit".equals(operation)) {
        	this.edit(request,response);
		}else if ("delete".equals(operation)) {
        	this.delete(request,response);
		}else if ("author".equals(operation)) {
        	this.author(request,response);
		}else if ("updateRoleModule".equals(operation)) {
        	this.updateRoleModule(request,response);
		}
	
	}

private void updateRoleModule(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		String roleId  = request.getParameter("roleId");
		String moduleIds  = request.getParameter("moduleIds");
		roleService.updateRoleModule(roleId,moduleIds);
        roleService.updateRoleModule(roleId, moduleIds);
        list(request, response);
		
	}
	
	private void author(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String roleId  = request.getParameter("id");
         Role role = roleService.findById(roleId);
         request.setAttribute("role", role);
         List<Map> map =   moduleService.findAuthorDataByRoleId(roleId);
         //System.out.print(map);
         ObjectMapper om = new ObjectMapper();
         String json = om.writeValueAsString(map);        
         request.setAttribute("roleModuleJson", json);		
		request.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		Role role = BeanUtil.fillBean(request, Role.class,"yyyy-MM-dd");
		RoleService roleService = new RoleServiceImp();
		roleService.delete(role);
		list(request, response);
		
	}

	private void edit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Role role = BeanUtil.fillBean(request, Role.class,"yyyy-MM-dd");
		
		RoleService roleService = new RoleServiceImp();
		roleService.update(role);
				
		list(request, response);
	}

	private void toEdit(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

		String id = request.getParameter("id");
		RoleService roleService = new RoleServiceImp();
		Role role = roleService.findById(id);
		request.setAttribute("role", role);
		List<Dept> all = deptService.findAll();
		request.setAttribute("deptList", all);
		request.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(request, response);
		//response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
		
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//进入列表页，查询全部数据，存入session，调用显示
		RoleService roleService = new RoleServiceImp();
		int page =1;
		int size=5;
	   if((request.getParameterValues("page"))!=null){
		   page = Integer.parseInt(request.getParameter("page"));
	   }
	   if((request.getParameterValues("size"))!=null){
		   size = Integer.parseInt(request.getParameter("size"));
	   }
		
		
		PageInfo all = roleService.findAll(page, size);
		
		request.setAttribute("page", all);
		
		request.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(request, response);
		
	}

	private void toAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Role> all = roleService.findAll();
		request.setAttribute("roleList", all);
		
		request.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(request, response);
		
	}
     private void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		Role role = BeanUtil.fillBean(request, Role.class,"yyyy-MM-dd");
		RoleService roleService = new RoleServiceImp();
		roleService.save(role);
		//response.sendRedirect(request.getContextPath()+"/system/role?operation=list");
		list(request, response);
		
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
